#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
using namespace std;
map<string,int> m;
const int g[]={0,256,256,256,256,65536};
int n;
string ch,st[1111];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>ch>>st[i];
		bool p=false;
		int o=1,e=0,di=0,mao=0,cnt=5,k=1;
		for(int j=st[i].length()-1;j>=0;j--)
		{
			if(st[i][j]>47&&st[i][j]<58)
			e+=k*(st[i][j]-48),k*=10;
			else
			{
				k=1;
				if(e>=g[cnt]){p=true;break;}
				cnt--;e=0;
				if(st[i][j+1]=='0')
				{
					if(!(j+2==st[i].length()))
					if(st[i][j+2]<58&&st[i][j+2]>47)
					{p=true;break;}
				}
				if((cnt==4&&st[i][j]!=':')||(cnt<4&&st[i][j]!='.')){p=true;break;}
				if(st[i][j]=='.'){di++;}
				if(st[i][j]==':')mao++;
			}
		}
		if(p||di!=3||mao!=1)
		{cout<<"ERR"<<endl;continue;}
		if(ch[0]=='S')
		{
			if(!m[st[i]])
			{m[st[i]]=i;cout<<"OK"<<endl;}
			else
			{cout<<"FAIL"<<endl;}
		}
		else
		{
			if(m[st[i]])
			{cout<<m[st[i]]<<endl;}
			else cout<<"FAIL"<<endl;
		}
	}
	return 0;
}
